﻿@page "/course/{CourseId:guid}/submission/{AssignmentId:guid}"
@layout ManagerLayout

@using Blazored.TextEditor

@inject IJSRuntime Js;
@inject IWebHostEnvironment Environment
@inject NavigationManager NavigationManager

@inject IMessageService MessageService
@inject IUserService UserService
@inject IAssignmentService AssignmentService
@inject ISubmissionService SubmissionService
@inject IFileService FileService



@*TODO:记录上一次提交结果*@

<Descriptions Title="作业信息" Bordered Size="@DescriptionsSize.Small">
    <DescriptionsItem Title="标题">@_assignment.Title</DescriptionsItem>
    <DescriptionsItem Title="截止日期">@_assignment.Deadline.ToString("yyyy-MM-dd")</DescriptionsItem>
    <DescriptionsItem Title="所属课程">@_assignment.Course.Name</DescriptionsItem>
    @if (_assignment.FileSetId != null && _assignment.FileSetId != Guid.Empty)
    {
        <DescriptionsItem Title="作业文件">
            <Button Icon="Download" OnClick="()=>DownloadFileSet(_assignment.FileSetId.Value)">下载</Button>
        </DescriptionsItem>
    }
    
</Descriptions>
<Divider></Divider>
<Text Strong Style="margin-bottom:16px">文本输入</Text>
<BlazoredTextEditor @ref="@_quillHtml" Placeholder="输入文本信息">
    <ToolbarContent>
        <select class="ql-header">
            <option selected=""></option>
            <option value="1"></option>
            <option value="2"></option>
            <option value="3"></option>
            <option value="4"></option>
            <option value="5"></option>
        </select>
        <span class="ql-formats">
            <button class="ql-bold"></button>
            <button class="ql-italic"></button>
            <button class="ql-underline"></button>
            <button class="ql-strike"></button>
        </span>
        <span class="ql-formats">
            <select class="ql-color"></select>
            <select class="ql-background"></select>
        </span>
        <span class="ql-formats">
            <button class="ql-list" value="ordered"></button>
            <button class="ql-list" value="bullet"></button>
        </span>
        <span class="ql-formats">
            <button class="ql-link"></button>
        </span>
        <span class="ql-formats">
            <button class="ql-script" value="sub"></button>
            <button class="ql-align" value="center"></button>
            <button class="ql-align" value="right"></button>

            <button class="ql-code-block"></button>
            <button class="ql-indent" value="+1"></button>
        </span>
    </ToolbarContent>
</BlazoredTextEditor>
<Divider></Divider>
<Text Strong Style="margin-bottom:16px">文件提交</Text>
<div>
    <InputFile id="fileInput" OnChange="UploadFiles" hidden multiple />
    <Upload Name="files" FileList="fileList">
        <label class="ant-btn" for="fileInput">
            <Icon Type="upload" />
            点击上传文件
        </label>
    </Upload>
</div>


<Divider></Divider>
<Button OnClick="@FinishAssignment">提交作业</Button>

@code {
    [Parameter]
    public Guid AssignmentId { get; set; }
    [Parameter]
    public Guid CourseId { get; set; }

    private BlazoredTextEditor _quillHtml;
    private string _quillHTMLContent;

    [CascadingParameter]
    private Task<AuthenticationState>? _authenticationState{ get; set; }


    private Assignment _assignment=new();
    private Models.Course _course = new();
    protected override async Task OnParametersSetAsync()
    {
        var response = await AssignmentService.GetByIdAsync(AssignmentId);
        if (response.Success)
        {
            _assignment =response.Data??new();
            _course = _assignment.Course;
        }       
        await base.OnParametersSetAsync();
    }


    private void DownloadFileSet(Guid fileSetId)
    {
        // TODO
    }

    List<UploadFileItem> fileList = new List<UploadFileItem>();


    IList<IBrowserFile> files = new List<IBrowserFile>();
    List<BrowserFile> _browserFiles = new();
    UploadFile _uploadFile = new();
    private bool _isLoading = false;


    private async void UploadFiles(InputFileChangeEventArgs e)
    {
        _browserFiles.Clear();
        files.Clear();
        _isLoading = true;
        foreach (var file in e.GetMultipleFiles())
        {
            if (file.Size < 1024 * 1024 * 100)
            {

                files.Add(file);
            }
            else
            {
                await MessageService.Warning($"{file.Name}文件体积过大(超过100MB)，无法上传");
            }
        }

        foreach (var file in files)
        {
            var fileName = file.Name;
            var fileSuffix = file.Name.Split(".").Last();
            string path="";
            byte[] fileContent =new byte[]{ };
            if (_assignment.AutoRename)
            {
                fileName = $"{_course.Name}-{_assignment.Title}-{_authenticationState.Result.User.FindFirst(System.Security.Claims.ClaimTypes.Name).Value}.{fileSuffix}";
            }

            //文件形式存入
            path = Path.Combine(Environment.ContentRootPath, "Resources", fileName);

            await using FileStream fs = new(path, FileMode.Create);
            // 修改
            await file.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024).CopyToAsync(fs);

            BrowserFile browserFile = new()
            {
                Name = fileName,
                FileType = file.ContentType,
                Size = file.Size,
                LastModified = file.LastModified.DateTime,
                Url = path,
            };

            _browserFiles.Add(browserFile);
        }


        fileList = files.Select(file => new UploadFileItem { FileName = file.Name, Size = file.Size }).ToList();

        _uploadFile = new()
        {
            UserId = Guid.Parse(_authenticationState.Result.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value),
            FilePurposeType = FilePurposeType.作业提交,
            browserFiles = _browserFiles,
        };

        StateHasChanged();
    }

    private async void FinishAssignment()
    {
        Guid? fileSetId=null;
        if (_uploadFile.browserFiles != null && _uploadFile.browserFiles.Any())
        {
            var response =  await FileService.UploadFilesAsync(_uploadFile);
            if (response.Success && response.Data!=null)
            {
                fileSetId = response.Data.FileSetId;
            }
            else
            {
                await  MessageService.Error("上传文件出现错误！");
                return;
            }
        }
        SubmissonInfo submissonInfo = new()
        {
            SubmissionTime = DateTime.Now,
            AssignmentId = _assignment.AssignmentId,
            StudentId = Guid.Parse(_authenticationState.Result.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value),
            RichText = await _quillHtml.GetHTML(),
            FileSetId = fileSetId
        };

        var subResponse =await SubmissionService.AddSubmissionsAsync(submissonInfo);
        if (subResponse.Success)
        {
            NavigationManager.NavigateTo($"course/{CourseId}");
            await  MessageService.Success("作业提交成功!");
        }
        else
        {
            await MessageService.Error("作业提交失败!");
        }
    }
}